//
//  DebugNetWorkManager.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/14.
//

#import "DebugNetWorkManager.h"

@interface DebugNetWorkManager (){
    dispatch_semaphore_t semaphore;
}

@property (nonatomic, strong, readwrite) NSMutableArray<DebugNetWorkModel *> *records;

@end

@implementation DebugNetWorkManager


static DebugNetWorkManager *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[DebugNetWorkManager alloc] init];
        BOOL result = [[[NSUserDefaults standardUserDefaults] objectForKey:@"k_dbmanager_networking_record"]?:@"1" boolValue] ;
        _instance.isRecordNetWorking = result;
    }
    return _instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _records = [NSMutableArray array];
        semaphore = dispatch_semaphore_create(1);
    }
    return self;
}

- (void)saveRecord:(DebugNetWorkModel *)record
{
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    [self.records insertObject:record atIndex:0];
    dispatch_semaphore_signal(semaphore);
}
- (void)removeItemAtIndex:(NSInteger)index
{
    if (index < self.records.count && index >= 0) {
        [self.records removeObjectAtIndex:index];
    }
}
- (void)removeAllDatas
{
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    [self.records removeAllObjects];
    dispatch_semaphore_signal(semaphore);
}
#pragma mark -----------getter/setter------------

- (void)setIsRecordNetWorking:(BOOL)isRecordNetWorking
{
    _isRecordNetWorking = isRecordNetWorking;
}


@end


@implementation DebugNetWorkModel

@end
